<?php


class user_module extends CI_Module {
	public function init() {
		$this->load->model('muser');
	}
	
	public function active() {
		
	}
	
	public function deactive() {
		
	}
	
	public function admin($config) {
		$this->view('admin', $config);
	}
	
	/**
	 * 
	 * @param object $config
	 * 
	 */
	public function run($config) {
		if ($this->muser->checkLogin()) {
			$this->view('user', $config);
		}
		else {
			$this->view('login', $config);
		}
	}
	
	public function loginAction ($param){
		$status = "-1";
		$account = $this->input->post('user_login', TRUE);
		$password = $this->input->post('password', TRUE);
		if ($_POST && $account && $password  ) {
			$user = $this->muser->get($account, 'use_account');
			if ($user) {
				list($_pass , $_salt) = explode(':', $user->use_password);
				$hased_pass = $this->muser->hash_pass($password, $_salt);
				
				if (isset($_pass) 
					&& $_pass
					&& $_pass == $hased_pass
				) {
					$status = "1";
					$this->muser->setLogin($user);
				}else {
					$status = "0";
				}
			}
			
		}
		echo $status;
		die;
	}
	
	public function logoutAction ($param){
		$this->muser->logout();
		die;
	}
	
	/**
	 * status 	0 value !exists
	 * 			1 value exists
	 * 			-1 data submit not allowed;
	 */
	public function existsAction ($param) {
		$allowfield = array('email', 'account');
		$status = "-1";
		if ($_POST 
		&& $this->input->post('field')
		&& $this->input->post('value')
		) {
			$field = $this->input->post('field', TRUE);
			$value = $this->input->post('value', TRUE);
			if (in_array($field, $allowfield)) {
				$field = 'use_'.$field;
				if (in_array($field, $this->muser->field())) {
					if ($this->muser->get($value, $field)) {
						$status = "1";
						} else {
						$status = "0";
					}
				}
			}
		}
		echo $status;
		die;
	}
	
	public function registerAction ($param) {
		$post = $this->input->post();
		$result = array (
			'code' => 0,
			'msg' => 'Chưa xử lí'
		);
		if ($post 
			&& $this->input->post('login')
			&& preg_match($this->muser->regex_username, $this->input->post('login'), $outuser)
			&& $this->input->post('email')
			&& preg_match($this->muser->regex_email, $this->input->post('email'), $outmail)
			&& $this->input->post('password')
			&& $this->input->post('confirm_password')
			&& $this->input->post('confirm_password') == $this->input->post('password')
		) {
			$data = array(
				'use_email'=> $this->input->post('email'),
				'use_account'=> $this->input->post('login'),
				'use_password'=> $this->input->post('confirm_password')
			);
			$reg = $this->muser->registry($data);
			if ($reg > 0) {
				$result['code'] = 1;
				$result['msg'] = 'Đăng ký thành công!';
				//set login
				$this->muser->setLogin($this->muser->get($reg));
			}else {
				$result ['code'] = -2;
				$result['msg'] = 'Quá trình đăng kí bị lỗi, Vui lòng thử lại!';
			}
		}else {
			$result['code'] = -1;
			$result['msg'] = 'Thông tin chưa chính xác.';
		}
		echo json_encode($result);
		die;
	}
	
	public function plusAction($param) {
		echo $param[0] + $param[1];
	}
}